# Author:: Alexandre Ribeiro de Souza (<alexandre@lumis.com.br>)

require 'right_aws'

::Chef::Recipe.send(:include, Opscode::Aws::Ec2)

aws = data_bag_item("aws", "knife-user")

unless node['lumis']['elastic_ip']
  aws_ec2 = RightAws::Ec2.new(aws['aws_access_key_id'], aws['aws_secret_access_key'])
  elastic_ip = aws_ec2.allocate_address
  Chef::Log.info(elastic_ip[:public_ip])
  node.set['lumis']['elastic_ip'] = elastic_ip[:public_ip]
  
  ruby_block "save-node-data" do
    block do
      node.save
    end
    action :create
  end
end

aws_elastic_ip "eip_#{Chef::Config[:node_name]}" do
  aws_access_key aws['aws_access_key_id']
  aws_secret_access_key aws['aws_secret_access_key']
  ip node['lumis']['elastic_ip']
  action :associate
end
